package com.chongzi.cloud;

import java.util.concurrent.ThreadLocalRandom;

public class codeTest {
    public static void main(String[] args) {
        int[] data={3,2,3,1,2,4,5,5,6};
        int a= findKthLargest(data,4);
        System.out.println(a);
    }
    public static int findKthLargest(int[] nums, int k) {
        qsort(nums,0,nums.length-1);
        return nums[nums.length-k];
    }

    private static void qsort(int[] nums, int left, int right){
        if (left>right){
            return ;

        }
        int index=partion(nums,left,right);
        qsort(nums,left,index-1);
        qsort(nums,index+1,right);

    }

    private static int partion(int[] nums, int left, int right) {
      int inx = ThreadLocalRandom.current().nextInt(right-left+1)+left;
       swap(nums,inx,left);
       int pv=nums[left];
       int i=left+1;
       int j=right;
      while (i<=j){
            while (i<=j&&nums[i]<pv){i++;}
            while (i<=j&&nums[j]>pv){j--;}
            if (i<=j){
                swap(nums,i,j);
                i++;
                j--;
            }
        }
        swap(nums,left,j);
        return  j;

    }
    private static void swap(int[] nums,int left,int idx){
        int temp=nums[left];
        nums[left]=nums[idx];
        nums[idx]=temp;

    }
}

